package com.victor.controller.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.entity.Personaltravel;
import com.victor.entity.TravelCollection;
import com.victor.entity.TravelComments;
import com.victor.entity.TravelFocus;
import com.victor.entity.TravelPraise;
import com.victor.entity.TravelReply;
import com.victor.entity.User;
import com.victor.service.TravelService;
import com.victor.utils.ClassFilter;
import com.victor.utils.Msg;

@RestController
@RequestMapping("persontravel")
public class TravelAppController {
	@Autowired
	private TravelService travelService;
	
	/**
	 *查询全部游记
	 * @param uid
	 * @param page
	 * @param row
	 * @return
	 */
	@RequestMapping("/all")
	public Msg findAll(String uid,int page,int row) throws Exception{
		if (uid == null || uid.isEmpty()) {
			PageInfo<Personaltravel> all = travelService.selAll(page, row);
			return Msg.success().add("info", all);
		}else {
			PageInfo<Personaltravel> byUid = travelService.selAllByUid(page,row,uid);
			return Msg.success().add("info", byUid);
		}
		
	}
	/**
	 * 各人游记-用户
	 * @param userid(游记用户id)
	 * @param uid(登陆的id)
	 * @return
	 */
	@RequestMapping("/user")
	public Msg selPersontravel(String userid,String uid) {
		if (uid == null || uid.isEmpty()) {
			User user = travelService.selUserByUserId(userid);
			return Msg.success().add("user", user);
		}else {
			User user = travelService.selUserByUid(uid,userid);
			return Msg.success().add("user", user);
		}
		
	}
	/**
	 * 各人游记-游记
	 * @param userid(游记用户id)
	 * @param uid(登陆的id)
	 * @param page
	 * @param row
	 * @return
	 */
	@RequestMapping("/alltravel")
	public Msg findAllTravel(String userid,String uid,int page,int row) {
		if (uid == null || uid.isEmpty()) {
			PageInfo<Personaltravel> info = travelService.selTravelBUserId(userid,page,row);
			return Msg.success().add("info", info);
		}else {
			PageInfo<Personaltravel> info = travelService.selTravelBUid(uid,userid,page,row);
			return Msg.success().add("info", info);
		}
		
	}
	/**
	 * 添加游记
	 * @param personaltravel
	 * @return
	 */
	@RequestMapping("/addtravel")
	public Msg addPersonTravel(@RequestBody Personaltravel personaltravel) {
		String[] strings = {"userid","username"};
		boolean empty = ClassFilter.somePropertyIsEmpty(personaltravel, strings);
		if (empty == true) {
			return Msg.fail("入参错误");
		}
		boolean b = travelService.insertTravel(personaltravel);
		if (!b) {
			return Msg.fail("添加失败");
		}
		if (personaltravel.getTravels() != null || personaltravel.getTravels().size()>0) {
			for (int i = 0; i < personaltravel.getTravels().size(); i++) {
				personaltravel.getTravels().get(i).setTravelid(String.valueOf(personaltravel.getId()));
				boolean insertPhoto = travelService.insertPhoto(personaltravel.getTravels().get(i));
				if (!insertPhoto) {
					return Msg.fail("添加游记图片失败");
				}
			}
		}
		return Msg.success();
		
	}
	
	/**
	 * 关注/取消关注
	 * @param travelFocus
	 * @return
	 */
	@RequestMapping("/focus")
	public Msg focus(TravelFocus travelFocus) {
		String[] strings = {"uid","pid"};
		boolean empty = ClassFilter.somePropertyIsEmpty(travelFocus, strings);
		if (empty == true ||travelFocus.getUid().equals(travelFocus.getPid())) {
			return Msg.fail("入参失败");
		}
		TravelFocus selByUidAndPid = travelService.selByUidAndPid(travelFocus);
		if (selByUidAndPid == null) {
			boolean insertFocus = travelService.insertFocus(travelFocus);
			if (!insertFocus) {
				return Msg.fail("添加关注失败");
			}
			return Msg.success().add("return", "关注成功");
		}else {
			boolean delFocus = travelService.delFocus(selByUidAndPid.getId());
			if (!delFocus) {
				return Msg.fail("删除关注失败");
			}
			return Msg.success().add("return", "取消关注成功");
		}
		
	}
	/**
	 * 点赞/取消点赞  游记
	 * @param travelPraise
	 * @return
	 */
	@RequestMapping("/praise")
	public Msg praiseTravel(TravelPraise travelPraise) {
		String[] strings = {"uid","travelid"};
		boolean empty = ClassFilter.somePropertyIsEmpty(travelPraise, strings);
		if (empty == true) {
			return Msg.fail("入参失败");
		}
		TravelPraise selPraiseByUidAndTravelid = travelService.selPraiseByUidAndTravelid(travelPraise);
		if (selPraiseByUidAndTravelid == null) {
			boolean insertPraise = travelService.insertPraise(travelPraise);
			if (!insertPraise) {
				return Msg.fail("添加游记点赞失败");
			}
			String count = travelService.selPraiseCount(travelPraise.getTravelid());
			return Msg.success().add("return", "点赞游记成功").add("praisecount", count);
		}else {
			boolean delPraise = travelService.delPraise(selPraiseByUidAndTravelid.getId());
			if (!delPraise) {
				return Msg.fail("删除点赞失败");
			}String count = travelService.selPraiseCount(travelPraise.getTravelid());
			return Msg.success().add("return", "取消点赞成功").add("praisecount", count);
		}
	}
	/**
	 * 收藏/取消收藏  游记
	 * @param collection
	 * @return
	 */
	@RequestMapping("/collecion")
	public Msg collection(TravelCollection collection) {
		String[] strings = {"uid","travelid"};
		boolean empty = ClassFilter.somePropertyIsEmpty(collection, strings);
		if (empty == true) {
			return Msg.fail("入参失败");
		}
		TravelCollection selCollectionByUidAndTravelid = travelService.selCollectionByUidAndTravelid(collection);
		if (selCollectionByUidAndTravelid == null) {
			boolean insertCollection = travelService.insertCollection(collection);
			if (!insertCollection) {
				return Msg.fail("添加收藏失败");
			}
			return Msg.success().add("return", "收藏成功");
		}else {
			boolean delCollection = travelService.delCollection(selCollectionByUidAndTravelid.getId());
			if (!delCollection) {
				return Msg.fail("删除收藏失败");
			}
			return Msg.success().add("return", "取消收藏");
		}
	}
	/**
	 * 评论游记
	 * @param comments
	 * @return
	 */
	@RequestMapping("/comments")
	public Msg comment(@RequestBody TravelComments comments) {
		String[] strings = {"uid","travelid"};
		boolean empty = ClassFilter.somePropertyIsEmpty(comments, strings);
		if (empty == true) {
			return Msg.fail("入参失败");
		}
		boolean insertComments = travelService.insertComments(comments);
		if (!insertComments) {
			return Msg.fail("添加评论失败");
		}
		if (comments.getCommentsPhotos() != null || comments.getCommentsPhotos().size()>0) {
			for (int i = 0; i < comments.getCommentsPhotos().size(); i++) {
				comments.getCommentsPhotos().get(i).setTcid(String.valueOf(comments.getId()));
				boolean insertCommentsPhoto = travelService.insertCommentsPhoto(comments.getCommentsPhotos().get(i));
				if (!insertCommentsPhoto) {
					return Msg.fail("添加评论图片失败");
				}
				
			}
			
		}
		return Msg.success();
	}
	/**
	 * 游记评论回复
	 * @param reply
	 * @return
	 */
	@RequestMapping("/reply")
	public Msg reply(TravelReply reply) {
		String[] strings = {"uid","pid"};
		boolean empty = ClassFilter.somePropertyIsEmpty(reply, strings);
		if (empty == true) {
			return Msg.fail("入参失败");
		}
		boolean insertReply = travelService.insertReply(reply);
		if (!insertReply) {
			return Msg.fail("添加回复失败");
		}
		return Msg.success();
	}
	/**
	 * 游记详情
	 * @param travelid
	 * @param uid
	 * @return
	 */
	@RequestMapping("/detailsup")
	public Msg detailsUp(String travelid,String uid) {
		if (travelid == null || travelid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		if (uid == null || uid.isEmpty()) {
			Personaltravel selDetailsUp = travelService.selDetailsUp(travelid);
			return Msg.success().add("info", selDetailsUp);
		}else {
			Personaltravel selDetailsUpByUid = travelService.selDetailsUpByUid(travelid,uid);
			return Msg.success().add("info", selDetailsUpByUid);
		}
		
	}
	/**
	 * 游记详情评论
	 * @param travelid
	 * @param page
	 * @param row
	 * @return
	 */
	@RequestMapping("/detailscomments")
	public Msg datailsDown(String travelid,int page,int row) {
		if (travelid == null || travelid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		PageInfo<TravelComments> selDetailsDown = travelService.selDetailsDown(travelid,page,row);
		return Msg.success().add("list", selDetailsDown);
	}
	
	
}
